/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Models.Enquiry;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


/**
 *
 * @author phuong
 */
public class EnquiryDAO {
    public static int addEnquiry(Enquiry en) {
        int result = 0;
        try {
            Connection con = DBConnection.myConnection();            
            PreparedStatement st = con.prepareStatement("Insert into Enquiry values(?,?,?,?,?,?,?)");
            if (en.getCompanyID() == 0) {
                st.setNull(1, java.sql.Types.INTEGER);
            } else {
                st.setInt(1, en.getCompanyID());
            }
            if (en.getDriverID() == 0) {
                st.setNull(2, java.sql.Types.INTEGER);
            } else {
                st.setInt(2, en.getDriverID());
            }
            st.setString(3, en.getName());
            st.setString(4, en.getPhone());
            st.setString(5, en.getEmail());            
            st.setDate(6, new java.sql.Date(en.getExpectedDate().getTime()));
            st.setString(7, en.getDescription());            
            result = st.executeUpdate();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
    
    static ResultSet rs = null;
    static java.sql.Statement stm = null;
    public static List<Enquiry> getListEnquiry() throws Exception{
        List list = new ArrayList();
        Connection con = DBConnection.myConnection();
        stm = con.createStatement();
        rs = stm.executeQuery("Select * from Enquiry");
        while(rs.next()){
            Enquiry en = new Enquiry(rs.getInt("ID"), rs.getInt("CompanyID"), rs.getInt("DriverID"), rs.getString("Name"), rs.getString("Phone"), rs.getString("Email"), rs.getString("Description"), rs.getDate("ExpectedDate"));
            list.add(en);
        }
        con.close();
        return list;                
    }
    
    public static void deleteEnquiryByID(int id) throws Exception{
        Connection con = DBConnection.myConnection();
        PreparedStatement st = con.prepareStatement("delete from Enquiry where ID="+ id);
        st.executeUpdate();        
    }
}
